package com.project.web.admin.service.core.impl;

import com.project.mapper.core.AdminMapper;
import com.project.mapper.core.AdminRoleMapper;
import com.project.model.core.Admin;
import com.project.model.core.AdminRole;
import com.project.web.admin.service.common.impl.BaseServiceImpl;
import com.project.web.admin.service.core.AdminService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("adminServiceImpl")
public class AdminServiceImpl extends BaseServiceImpl<Admin, Long> implements AdminService {

    private static final long serialVersionUID = 3829959660675598663L;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Override
    public void saveAdminCascadeRole(Admin admin, Long[] roleIds) {

        //保存admin对象
        super.save(admin);

        //封装adminRole对象List集合
        Long adminId = admin.getId();
        List<AdminRole> adminRoleList = getAdminRoleList(roleIds, adminId);
        if (adminRoleList != null) {
            //批量保存adminRoleList
            adminRoleMapper.saveAdminRoles(adminRoleList);
        }

    }

    @Override
    public List<Admin> getAdminCascadeRole(Admin admin) {
        return adminMapper.getAdminCascadeRole(admin);
    }

    @Override
    public void updateAdminCascadeRoles(Admin admin, List<Long> delIds, Long[] saveIds) {
        //更新admin自有属性
        super.update(admin);
        Long adminId = admin.getId();
        List<AdminRole> adminRoleList = getAdminRoleList(delIds, adminId);
        //构建删除admin_role级联关系对象集合
        if (adminRoleList != null) {
            adminRoleMapper.delAdminRoles(adminRoleList);
        }

        List<AdminRole> adminRoleListForSave = getAdminRoleList(saveIds, adminId);
        //构建保存admin_role级联关系对象集合
        if (adminRoleListForSave != null) {
            adminRoleMapper.saveAdminRoles(adminRoleListForSave);
        }
    }

    @Override
    public void delAdminCascadeRoles(Long adminId, List<Long> delRoleIds) {
        List<AdminRole> adminRoleList = getAdminRoleList(delRoleIds, adminId);
        //构建删除admin_role级联关系对象集合
        if (adminRoleList != null) {
            adminRoleMapper.delAdminRoles(adminRoleList);
        }
        //删除admin
        super.delete(adminId);
    }

    /**
     * 查询数量判断是否重复
     *
     * @param admin
     * @return
     */
    public Boolean getCountForOnly(Admin admin) {
        return adminMapper.getCountForOnly(admin) == 0;
    }

    /**
     * 循环构建用户角色集合（提取的代码）
     *
     * @param ids
     * @param adminId
     * @return
     */
    private List<AdminRole> getAdminRoleList(List<Long> ids, Long adminId) {

        List<AdminRole> list = new ArrayList<AdminRole>();

        if (CollectionUtils.isNotEmpty(ids)) {
            for (Long id : ids) {
                addList(list, id, adminId);
            }
            return list;
        }
        return null;

    }

    /**
     * 循环构建用户角色集合（提取的代码）
     *
     * @param ids
     * @param adminId
     * @return
     */
    private List<AdminRole> getAdminRoleList(Long[] ids, Long adminId) {

        List<AdminRole> list = new ArrayList<AdminRole>();

        if (ArrayUtils.isNotEmpty(ids)) {
            for (Long id : ids) {
                addList(list, id, adminId);
            }
            return list;
        }
        return null;

    }

    /**
     * 构建用户角色集合（提取的代码）
     *
     * @param list
     * @param id
     * @param adminId
     */
    private void addList(List<AdminRole> list, Long id, Long adminId) {
        AdminRole adminRole = new AdminRole();
        adminRole.setAdminId(adminId);
        adminRole.setRoleId(id);
        list.add(adminRole);
    }

    /**
     * 用户登陆i专用
     *
     * @param admin
     * @return
     */
    @Override
    public List<Admin> selectListForLogin(Admin admin) {
        return adminMapper.selectListForLogin(admin);
    }

}
